package fr.epsi.spring1.dao.tests;

import java.util.List;

import org.apache.log4j.Logger;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import fr.epsi.spring1.domain.Article;
import fr.epsi.spring1.service.ArticleManager;

public class TestArticleManagerWithSpring {

	private final static Logger logger = Logger.getLogger(TestArticleManagerWithSpring.class);

	private static ArticleManager articleManager;

	@BeforeClass
	public static void init() {
		logger.error("Init test userManager");
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("testApplicationContext.xml");
		articleManager = context.getBean(ArticleManager.class);
	}

	@Test
	public void testInsert() {
		logger.error("Insert test");
		Article article = new Article();
		article.setReference(null);
		if (articleManager.insert(article)) {
			Assert.fail();
		}
	}

	@Test
	public void testUpdate() {
		logger.error("Update test");
		Article article = new Article();
		article.setReference("1");
		article.setPrix(Float.valueOf(400));
		article.setEpuise(false);
		articleManager.update(article);
	}

	@Test
	public void testDoubleInsert() {
		logger.error("Update test");
		Article article = new Article();
		article.setReference("1");
		article.setPrix(Float.valueOf(400));
		article.setEpuise(false);
		articleManager.insert(article);

		Article article2 = new Article();
		article.setReference("1");
		article.setPrix(Float.valueOf(300));
		article.setEpuise(false);
		if (articleManager.insert(article2)) {
			Assert.fail();
		}

	}

	@Test
	public void testModifSiEpuise() {
		logger.error("Update test");
		Article article = new Article();
		article.setReference("EXP");
		article.setPrix(Float.valueOf(400));
		article.setEpuise(true);
		articleManager.insert(article);

		article.setPrix(Float.valueOf(300));
		if (articleManager.update(article)) {
			Assert.fail();
		}

	}
	
	@Test
	public void testSelectVide() {
		logger.error("Update test");
		List<Article> listOfArticles = articleManager.rechercher("XXX");

		if (listOfArticles != null && listOfArticles.size() > 0) {
			Assert.fail();
		}

	}
	
	@Test
	public void testSelectAvecResultat() {
		logger.error("Update test");
		List<Article> listOfArticles = articleManager.rechercher("1");

		if (listOfArticles == null || listOfArticles.size() == 0) {
			Assert.fail();
		}

	}
		

}
